<?php

// {{{ includes
require dirname(dirname(__FILE__)).'/clicommand_bootstrap.php';
// }}}

class Cli_UpdateLikeStat extends Clicommand_Abstract {

    protected $_argv_pattern = array(
        '?size|-size',
        '?from|-f',
        '?to|-t',
        '*options|-o',
    );

    function execute() {
        set_time_limit(0);
        $key="update_linkstats_lock";
        Q::writeCache($key,array("locked"),array('life_time'=>3600*12));//加锁,防止重复执行
        $locked = Q::cache($key);
        if($locked){
//            die("已经在执行中!\n");
        }

        $facebook = $this->facebook();
        $base_url = "http://apps.facebook.com/orzwiki/view";
        //获取所有记录数
        $orz_count = Orz::find("status = 0")->getCount();
//        die($orz_count);
        $page_size = 100;
        $pages = ceil($orz_count/$page_size);
        for($page = 1;$page<=$pages;$page++){
            $orz_ids = Orz::find("status = 0")->limitPage($page,$page_size)->order("id DESC")->getCol("id",false);
            $urls = array();
            foreach($orz_ids as $id){
                $url = '"'.$base_url."/{$id}".'"';
                array_push($urls,$url);
            }
            $urls = implode(",", $urls);
            $fql = "SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url IN ({$urls})";
            $data = $facebook->api(array(
                'method' => 'fql.query',
                'query' => $fql
            ));
            //根据数据更新库
            foreach($data as $k=>$v){
                $orz_id = $orz_ids[$k];
                //判断数值是否>0,否则不更新
                foreach($v as &$vv){
                    if($vv==0){
                        unset($vv);
                    }else{
                        continue;
                    }
                }
                if(empty($v)){
                    continue;
                }
                Orz::meta()->updateDbWhere($v,"id=?",$orz_id);
            }
        }

        Q::cleanCache($key);
        
        echo "done!\n";
        
    }

    /**
     * 显示帮助信息
     */
    static function help() {
        echo <<<EOT

php cli_updatelikestat.php  [...]

syntax:
    php cli_updatelikestat.php [-size pagesize] [-f from] [-t to] [-o option ...]

examples:
    php cli_updatelikestat.php myapp -size 1000 -f 0 -t 10000



EOT;
    }

    function facebook(){
        try{
            $facebook = Q::registry("facebook");
        }catch(Exception $ex){
            //导入库目录
            Q::import(dirname(__FILE__) . "/../../lib/");
            Q::loadClass("Facebook");
            //ssl相关
            Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
            Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

            $facebook = new Facebook(array(
                'appId' => Q::ini("appini/facebook/appid"),
                'secret' => Q::ini("appini/facebook/secret"),
                'cookie' => true,
            ));
            Q::register($facebook,"facebook");
        }
        return $facebook;
    }
}

// 执行命令行脚本
clicommand_bootstrap('Cli_UpdateLikeStat');

